import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home/index",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    children: [
      {
        path: "index",
        name: "/home/index", // 不重复即可
        component: () => import("../views/Index.vue"),
      },
      {
        path: "AdministratorList",
        name: "/home/AdministratorList", // 不重复即可
        component: () => import("../views/Administrator/AdministratorList.vue"),
        meta: {
          nav: ["首页", "管理员列表"],
        },
      },
      {
        path: "AgentList",
        name: "/home/Agent", // 不重复即可
        component: () => import("../views/Agent/AgentList.vue"),
        meta: {
          nav: ["首页", "公司"],
        },
      },
      {
        path: "EmployeesList",
        name: "/home/EmployeesList", // 不重复即可
        component: () => import("../views/Employees/EmployeesList.vue"),
        meta: {
          nav: ["首页", "员工列表"],
        },
      },
      {
        path: "EmployeesAdd",
        name: "/home/EmployeesAdd", // 不重复即可
        component: () => import("../views/Employees/EmployeesAdd.vue"),
        meta: {
          nav: ["首页", "新人入职"],
        },
      },
      {
        path: "ClientList",
        name: "/home/ClientList", // 不重复即可
        component: () => import("../views/Client/ClientList.vue"),
        meta: {
          nav: ["首页", "用户列表"],
        },
      },
      {
        path: "ClientAdd",
        name: "/home/ClientAdd", // 不重复即可
        component: () => import("../views/Client/ClientAdd.vue"),
        meta: {
          nav: ["首页", "新增用户"],
        },
      },
      {
        path: "/home/ProductList/news",
        name: "/home/ProductList/news", // 不重复即可
        component: () => import("../views/Product/News.vue"),
        meta: {
          nav: ["首页", "产品管理", "新品推荐"],
        },
      },
      {
        path: "/home/ProductList/star",
        name: "/home/ProductList/star", // 不重复即可
        component: () => import("../views/Product/Star.vue"),
        meta: {
          nav: ["首页", "产品管理", "明星产品"],
        },
      },
      {
        path: "/home/ProductList/lip",
        name: "/home/ProductList/lip", // 不重复即可
        component: () => import("../views/Product/Lip.vue"),
        meta: {
          nav: ["首页", "产品管理", "唇妆"],
        },
      },
      {
        path: "/home/ProductList/eyes",
        name: "/home/ProductList/eyes", // 不重复即可
        component: () => import("../views/Product/Eyes.vue"),
        meta: {
          nav: ["首页", "产品管理", "眼妆"],
        },
      },
      {
        path: "/home/ProductList/basemakeup",
        name: "/home/ProductList/basemakeup", // 不重复即可
        component: () => import("../views/Product/BaseMakeup.vue"),
        meta: {
          nav: ["首页", "产品管理", "底妆"],
        },
      },
      {
        path: "/home/ProductList/brush",
        name: "/home/ProductList/brush", // 不重复即可
        component: () => import("../views/Product/Brush.vue"),
        meta: {
          nav: ["首页", "产品管理", "刷具"],
        },
      },
      {
        path: "/home/ProductList/skin",
        name: "/home/ProductList/skin", // 不重复即可
        component: () => import("../views/Product/Skin.vue"),
        meta: {
          nav: ["首页", "产品管理", "护肤"],
        },
      },
      {
        path: "ResultsR",
        name: "/home/ResultsR", // 不重复即可
        component: () => import("../views/Results/ResultsR.vue"),
        meta: {
          nav: ["首页", "营收额"],
        },
      },
    ],
  },
  {
    path: "/user/login",
    name: "/user/login",
    component: () => import("../views/user/Login.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

import store from "../store";
// 全局前置导航守卫,每当路由跳转到目标组件之前会自动执行箭头函数
router.beforeEach((to, from, next) => {
  console.log(`from:${from} to:${to}`);
  if (to.path == "/user/login") {
    next(); // 如果希望继续正常的跳转流程,则调用next()即可
  } else {
    // 如果不是跳转到登录页面,则需要验证vuex中是否有登录用户
    if (store.state.user) {
      // 如果已经登录,可以跳转
      next();
    } else {
      // 如果没有登录,则强制跳转到登录页
      router.push("/user/login");
    }
  }
});

export default router;
